"""
ECSHOP演示站
1. 首页搜索操作
2. 注册操作
3. 登录操作
"""
import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class Ecshop:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://ecshop.test2.shopex123.com/')

    def search(self):
        # 搜索操作
        # 找到搜索框位置输入搜索信息
        self.driver.find_element(By.NAME, 'keywords').send_keys('这是搜索框的位置')
        # 找到搜索提交按钮并点击
        self.driver.find_element(By.CLASS_NAME, 'btn_search').click()

    def sign_in(self, uname, e_mail, pwd, cpwd):
        # 注册操作
        # 找到注册按钮并点击
        self.driver.find_element(By.LINK_TEXT, '[ 免费注册 ]').click()
        time.sleep(2)
        # 输入用户名,邮箱,密码,确认密码,点击同意协议并注册
        self.driver.find_element(By.ID, 'username').send_keys(uname)
        self.driver.find_element(By.XPATH, '//*[@id="email"]').send_keys(e_mail)
        self.driver.find_element(By.ID, 'password1').send_keys(pwd)
        self.driver.find_element(By.ID, 'confirm_password').send_keys(cpwd)
        self.driver.find_element(By.CLASS_NAME, 'signup_button').click()

    def log_in(self, uname, pwd):
        # 登录操作
        # 找到登录按钮并点击
        self.driver.find_element(By.LINK_TEXT, '[ 请登录 ]').click()
        time.sleep(2)
        # 输入用户名,密码,勾选保存登录信息,点击登录
        self.driver.find_element(By.NAME, 'username').send_keys(uname)
        self.driver.find_element(By.NAME, 'password').send_keys(pwd)
        self.driver.find_element(By.ID, 'remember').click()
        self.driver.find_element(By.NAME, 'submit').click()

    def quit(self):
        self.driver.quit()


if __name__ == '__main__':
    ecshop = Ecshop()
    # ecshop.search()
    # ecshop.sign_in('zhang', '2868266666@qq.com', '123456', '123456')
    ecshop.log_in('zhang', '123456')

    time.sleep(3)
    ecshop.quit()
